function sum(...args) {
    const result = args.reduce((prev, cur) => prev + cur)
    return (...args) => {
        if(args.length === 0) return result
        return sum(result, ...args)
    }
}

let res = sum(1)(2, 3)(4, 5, 6)(7)()
console.log(res)